<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Models\User;
use App\Http\Requests\UserRequest;
use App\Handlers\ImageUploadHandler;
use Auth;


class UsersController extends Controller
{

  //权限
  public function __construct()
  {

      $this->middleware('auth', ['except' => ['show']]);
  }


    //show方法
    public function show(User $user)
    {


       return view('users.show',compact('user'));
    }

   //edit方法
   public function edit(User $user)
   {

     $this->authorize('update', $user);
     return view('users.edit',compact('user'));
   }

   //update方法
  public function update(UserRequest $request, ImageUploadHandler  $uploader, User  $user)
  {
    //执行更新操作
    $this->authorize('update', $user);
    $data=$request->all();
    if($request->avatar){
       $result=$uploader->save($request->avatar,"avatars",$user->id,362);
       if($result){
         $data['avatar']=$result['path'];
       }

    }
    $user->update($data);
    return redirect()->route('users.show', $user->id)->with('success', '个人资料更新成功！');
  }

}
